'use strict';

app.controller('reportFormCtrl', ['$scope', '$http', '$stateParams', '$rootScope', '$modal', 'lodash', '$location', '$anchorScroll','$timeout', function($scope, $http, $stateParams, $rootScope, $modal, lodash, $location, $anchorScroll,$timeout) {
	$rootScope.initBreadcrumb($scope.breadcrumb);
	$rootScope.initHeaderElements($scope.settings);
	var backUrl = {
        show : true,
        label: "返回",
        func : function() {
            $rootScope.backHistory();
        }
    };
    $scope.settings.btnEle.push(backUrl);
    $scope.info={};
    $scope.info.reportFormList=[];
    $timeout(function () {
        initFileInput();
    }, 500);

    $scope.reqDetails = {};
    var url = matrix_product_url +"/brdRequirement/getBrdRequirements?withFeatureDesc=0&reqId="+ $stateParams.reqId;
    $http.get(url).success(function(result) {
        if(result.code == "0") {
            if(result.data) {
                $scope.reqDetails = result.data[0];
                $scope.breadcrumb.push('产品设计',$scope.reqDetails.reqName);
            }
        }
    });

    $scope.getReportForm=function(){
        var url=matrix_product_url+"/brdRequirement/getReqReportFormList?reqId="+$stateParams.reqId;
        $http.get(url).success(function(result){
            if(result.code == "0") {
                $scope.info.reportFormList=result.data;
            } else {
                $rootScope.toasterErrorInfo(result.desc);
            }

        })
    }
    $scope.getReportForm();
    //下载文件
    $scope.downloadFile=function(fileId){
        window.location.href =matrix_product_url+"/brdRequirement/getReqReportFormFile?fileId="+fileId;
    }
    //删除文件
    $scope.deleteFile=function(file){
        console.log(file)
        var confirmParams = {
            "titleData": "提示",
            "bodyData": "确定删除【" + file.fileName + "】文件吗？"
        };
        $rootScope.confirmWindow(confirmParams, function (data) {
            var url=matrix_product_url+"/brdRequirement/deleteReqReportForm?fileId="+file.id;
            $http.get(url).success(function(result){
                if(result.code == "0") {
                    lodash.remove($scope.info.reportFormList, {id: file.id})
                    $rootScope.toasterSuccessInfo("删除成功");
                } else {
                    $rootScope.toasterErrorInfo(result.desc);
                }

            })
        })
    }
    function initFileInput(allowedFileExtensions) {
            var input = $("#testFile");
            input.fileinput({
                language: 'zh', //设置语言
                uploadUrl: matrix_product_url + '/brdRequirement/reqReportForm?reqId='+$stateParams.reqId,
                showUpload: true,
                uploadAsync: true,
                // allowedFileExtensions : ['jpg', 'png','gif'],//接收的文件后缀
                // allowedFileExtensions: allowedFileExtensions,//接收的文件后缀
                uploadExtraData: function () {
                    return {
                        resourceType: 'form'
                    };
                }
            }).on("fileuploaded", function (event, data, previewId, index) {
                $scope.$apply(function () {
                    if (data.response.code == '0') {
                       $scope.info.reportFormList.push(data.response.data);
                       $rootScope.toasterSuccessInfo("上传成功");
                    }else{
                        $rootScope.toasterErrorInfo(data.response.desc);
                    };
                });
            });
        };
}])
